import { createStore } from "vuex";
import { getList, userAdd, delById } from "@/Service/UserService";
// 创建一个新的 store 实例
const store = createStore({
  state: {
    num: 0,
    list: [],
  },
  mutations: {
    resetlist(state, payload) {
      state.list = payload;
    },
    dellist(state, id) {
      state.list = state.list.filter((item) => {
        return item.id != id;
      });
    },
    addlist(state, payload) {
      state.list.push(payload);
    },
  },
  actions: {
    async addUser(state, payload) {
      console.log(1);
      let list = userAdd(payload).then((res) => {
        console.log(res);
        state.commit("addlist", res);
      });
    },
    async dellist({ commit }, id) {
      delById(id);
      commit("dellist", id);
    },
    async resetlist({ commit }) {
      const res = await getList();
      commit("resetlist", res);
    },
  },
});

export default store;
